package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.car.actionform.CarSearchTransferInputForm;
import com.tianqu.tms.www.car.viewhelper.CarTransferViewHelper;

/**
 * @author zhangxueliang
 * 高级搜索过户信息修改中间过渡action
 */
public class CarSearchTransferInputDispatchAction extends TmsAction{

	private Logger logger = Logger.getLogger(CarSearchTransferInputDispatchAction.class);
	private static final String TORESULT = "toResult";//保存
	private static final String TOBACK = "toBack";//上一步
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String forwardPath = "";
		CarSearchTransferInputForm transferForm = (CarSearchTransferInputForm)form;
		CarTransferViewHelper transferHelper = new CarTransferViewHelper();
		transferHelper = (CarTransferViewHelper)uc.getAttribute(TmsConstants.TRANSFERHELPER);
		transferHelper = formToViewHelper(transferHelper,transferForm);
		uc.setAttribute(TmsConstants.TRANSFERHELPER, transferHelper);
		//跳转到确认页
		if(transferForm.getSaveInputInfo()!=null && !"".equals(transferForm.getSaveInputInfo()))
		{
			forwardPath = TORESULT;
		}else if(transferForm.getToBack() != null && !"".equals(transferForm.getToBack())){
			forwardPath = TOBACK;
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(forwardPath);
	}
	
	/**
	 * form->viewhelper
	 * @param viewHelper
	 * @param form
	 * @return
	 * @throws TmsDaoException 
	 */
	private CarTransferViewHelper formToViewHelper(CarTransferViewHelper transferHelper,CarSearchTransferInputForm form) throws TmsDaoException{
		transferHelper.setNewAgentAddress(form.getNewAgentAddress());
		transferHelper.setNewAgentName(form.getNewAgentName());
		transferHelper.setNewAgentOwnerIdCardNo(form.getNewAgentOwnerIdCardNo());
		transferHelper.setNewAgentPhone(form.getNewAgentPhone());
		transferHelper.setNewcarOwner(form.getNewcarOwner());
		transferHelper.setNewcarOwnerIdCardNo(form.getNewcarOwnerIdCardNo());
		transferHelper.setNewOwnerAddress(form.getNewOwnerAddress());
		transferHelper.setNewOwnerOrgNo(form.getNewOwnerOrgNo());
		transferHelper.setNewOwnerType(form.getNewOwnerType());
		if(transferHelper.getPriceFlag() == 2){
			if("0".equals(form.getPrice())){
				transferHelper.setPrice("");
			}else{
				transferHelper.setPrice(form.getPrice());
			}
		}
		
		Codes code = Codes.getInstance();
		transferHelper.setNewOwnerTypeList(code.getCodeslistByItemId(Codes.CAR_OWNER));
		transferHelper.setTransferTypeList(code.getCodeslistByItemId(Codes.TRANSFER_TYPE));
		transferHelper.setGainTypeList(code.getCodeslistByItemId(Codes.GAIN_TYPE));
		transferHelper.setGainType(form.getGainType());
		transferHelper.setTransferType(form.getTransferType());
		transferHelper.setTaxNo(form.getTaxNo());
		transferHelper.setMailAddress(form.getMailAddress());
		transferHelper.setMoveOutCity(form.getMoveOutCity());
		transferHelper.setMoveOutProvince(form.getMoveOutProvince());
		transferHelper.setCarMarketIn(form.getCarMarketIn());
		transferHelper.setNewOwnerPhone(form.getNewOwnerPhone());
		transferHelper.setZipCode(form.getZipCode());
		transferHelper.setNewOwnerTelPhone(form.getNewOwnerTelPhone());
		return transferHelper;
	}

}
